package org.gameg.service.biz.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.gameg.bean.BetPower;
import org.gameg.bean.Game;
import org.gameg.bean.Player;
import org.gameg.bean.Power;

import top.ibase4j.core.exception.BusinessException;
import top.ibase4j.core.util.DataUtil;
import top.ibase4j.core.util.MathUtil;

/**
 * 点数
 * @author ShenHuaJie
 */
public class PointHelper {
    static final Logger logger = LogManager.getLogger();

    public static final String[] g = {"", "牛1", "牛2", "牛3", "牛4", "牛5", "牛6", "牛7", "牛8", "牛9", "牛牛", "金牛", "对子", "顺子",
        "倒顺", "满牛", "豹子","大", "小", "单", "双", "合", "大单", "小单", "大双", "小双"};

    /**
     * 获取点数
     * @param config 配置(dt位数<1:分2:角分3:圆角分>;<br>
     * jnt金牛种类<1代表：0.10,0.20,0.30,0.40到0.90; 2代表：0.10,1.10,2.10,3.10到9.10; 3代表：0.10,1.10>;<br>
     * dzt对子种类<1代表：0.11,0.22,0.33.0.44到0.99; 2代表：0.11,0.22,0.33.0.44...0.99,1.22,1,33..1.99...2.11..2.33..2.99到9.88>;<br>
     * szt顺子种类<1代表：0.12,1.23,2.34,3.45到7.89; 2代表：1.23,2.34,3.45,4.56,到7.89>)
     * @param amount 金额
     * @return 对应规则的比重值(可比较大小)
     */
    public static Integer getPoint(Game config, Map<Integer, Power> powers, BigDecimal amount) {
        String digit = DataUtil.ifNull(config.getDt(), "3");
        String jinniuType = DataUtil.ifNull(config.getJnt(), "2");
        String duiziType = DataUtil.ifNull(config.getDzt(), "2");
        String shunziType = DataUtil.ifNull(config.getSzt(), "2");
        logger.info("参数：取{}位数算牛，第{}种金牛，第{}种对子，第{}种顺子", digit, jinniuType, duiziType, shunziType);
        BigDecimal shi = new BigDecimal("10");
        int a = amount.intValue() % 10;
        int b = amount.subtract(new BigDecimal(String.valueOf(amount.intValue()))).multiply(shi).intValue();
        int c = amount.multiply(new BigDecimal("100")).remainder(shi).intValue();
        Integer p, result;
        if (powers.get(16).getIsOpen() && a == b && a == c) { // 豹子
            p = 16;
        } else if (powers.get(15).getIsOpen() && a != 0 && b == 0 && c == 0) { // 满牛
            p = 15;
        } else if (powers.get(14).getIsOpen() && a - b == 1 && b - c == 1 && c != 0) { // 倒顺
            p = 14;
        } else if (powers.get(13).getIsOpen() && "1".equals(shunziType) && c - b == 1 && b - a == 1
                || "2".equals(shunziType) && a != 0 && c - b == 1 && b - a == 1) { // 顺子
            p = 13;
        } else if (powers.get(12).getIsOpen()
                && ("1".equals(duiziType) && a == 0 && b == c || "2".equals(duiziType) && b == c)) { // 对子
            p = 12;
        } else if (powers.get(11).getIsOpen()
                && ("1".equals(jinniuType) && a == 0 && c == 0 || "2".equals(jinniuType) && b == 1 && c == 0
                || "3".equals(jinniuType) && (amount.doubleValue() == 0.10 || amount.doubleValue() == 1.10))) { // 金牛
            p = 11;
        } else {
            int d = -1;
            if ("1".equals(digit)) {
                d = c;
            } else if ("2".equals(digit)) {
                d = b + c;
            } else if ("3".equals(digit)) {
                d = a + b + c;
            } else {
                throw new RuntimeException("计算位数设置错误.");
            }
            if (d > 10) {
                d = d % 10;
            }
            if (d == 0) {
                d = 10;
            }
            p = d;
        }
        result = powers.get(p).getNiuPoint();
        logger.info("红包：" + amount + "====>点数：" + p + "=>牌型：" + g[p] + "=>牛数：" + result);
        return p;
    }


    /**
     * 庄家闲家比大小
     * @param game  游戏配置
     * @param a   庄家
     * @param b   闲家
     * @return
     */
    public static int compare(Game game, Player a, Player b) {
        if (game.getRsMax() != null && game.getRsMax() >= b.getPoint()) {
            return 1; // 几点以下认输
        }
        if (a.getAmount().doubleValue() == b.getAmount().doubleValue()) {
            if (game.getDpMinAmount() != null && game.getDpMinAmount() != 0 && game.getDpMinAmount() <= b.getPoint()) {
                return 0; // 几点以上同金额打平(至豹子)
            }
        }
        if (a.getPoint() == b.getPoint()) {
            if (game.getDpMax() != null && game.getDpMax() >= b.getPoint()) {
                return 1; // 几点以下同点a赢
            }
            if (game.getDpMinPoint() != null && game.getDpMinPoint() != 0 && game.getDpMinPoint() <= b.getPoint()) {
                return 0; // 几点以上同点打平(至牛牛)
            }
        }
        //同对子处
        if ("对子".equals(PointHelper.g[Integer.valueOf(a.getPoint())])&&"对子".equals(PointHelper.g[Integer.valueOf(b.getPoint())])) {//闲和庄都是对子
            switch (game.getTdzb()){
                case "1":
                    logger.warn("闲家{}和庄家{}同对子，比金额", b.getWxId(), a.getWxId());
                    if (a.getAmount().doubleValue() > b.getAmount().doubleValue()) {
                        return 1;
                    } else if (a.getAmount().doubleValue() == b.getAmount().doubleValue()) {
                        return 0;
                    } else {
                        return -1;
                    }
                case "2":
                    logger.warn("闲家{}和庄家{}同对子，比尾数", b.getWxId(), a.getWxId());
                    Integer taila = Integer.valueOf(String.valueOf(a.getAmount()).substring(String.valueOf(a.getAmount()).length()-1));
                    Integer tailb = Integer.valueOf(String.valueOf(b.getAmount()).substring(String.valueOf(b.getAmount()).length()-1));
                    if (taila > tailb) {
                        return 1;
                    } else if (tailb == taila) {
                        return 0;
                    } else {
                        return -1;
                    }
                case "3":
                    logger.warn("闲家{}和庄家{}同对子，先尾后金额", b.getWxId(), a.getWxId());
                    Integer taila2 = Integer.valueOf(String.valueOf(a.getAmount()).substring(String.valueOf(a.getAmount()).length()-1));
                    Integer tailb2 = Integer.valueOf(String.valueOf(b.getAmount()).substring(String.valueOf(b.getAmount()).length()-1));
                    if (taila2 > tailb2) {
                        return 1;
                    } else if (tailb2 == taila2) {
                        if (a.getAmount().doubleValue() > b.getAmount().doubleValue()) {
                            return 1;
                        } else if (a.getAmount().doubleValue() == b.getAmount().doubleValue()) {
                            return 0;
                        } else {
                            return -1;
                        }
                    } else {
                        return -1;
                    }
            }
        }
        int aPoint = a.getNiuPoint();
        int bPoint = b.getNiuPoint();
        if (aPoint > bPoint) { // a赢
            return 1;
        } else if (bPoint > aPoint) { // b赢
            return -1;
        } else { // 同点
            double aAmount = a.getAmount().doubleValue();
            double bAmount = b.getAmount().doubleValue();
            switch (game.getSamePoint()) {
            case "1": // 比金额
                return compare(aAmount, bAmount);
            case "2": // 比角分
                int aJF = MathUtil.multiply(a.getAmount(), 100).intValue() % 100;
                int bJF = MathUtil.multiply(b.getAmount(), 100).intValue() % 100;
                if ("Y".equals(game.getCmpAmount()) && aJF == bJF) { // 比金额
                    return compare(aAmount, bAmount);
                }
                return compare(aJF, bJF);
            case "3": // 比分
                int aF = MathUtil.multiply(a.getAmount(), 100).intValue() % 10;
                int bF = MathUtil.multiply(b.getAmount(), 100).intValue() % 10;
                if ("Y".equals(game.getCmpFrontOne()) && aF == bF) { // 比角分
                    aJF = MathUtil.multiply(a.getAmount(), 100).intValue() % 100;
                    bJF = MathUtil.multiply(b.getAmount(), 100).intValue() % 100;
                    if (aJF == bJF) { // 比金额
                        return compare(aAmount, bAmount);
                    }
                    return compare(aJF, bJF);
                }
                return compare(aF, bF);
            case "4": // 庄赢
                return 1;
            case "5": // 打平
                return 0;
            }
            throw new BusinessException("配置错误");
        }
    }

    /**
     * 庄家闲家比大小
     * @param game  游戏配置
     * @param a   庄家
     * @param b   闲家
     * @return 1闲输 -1闲赢 0：平局
     */
    public static int compare(Game game, Player a, Player b, String type) {//分三种情况 庄闲   和  如果是和 ，出现同点 就是和 ， 庄闲 ，则是比到底。

        if (b.getBetPoint().equals(36) || b.getBetPoint().equals(37)) {//庄闲比到底
            int aPoint = a.getPoint();
            int bPoint = b.getPoint();
            if (aPoint > bPoint) { // 庄赢
                if (b.getBetPoint().equals(36)) {//买庄
                    return -1;
                }
                if (b.getBetPoint().equals(37)) {//买闲
                    return 1;
                }
            } else if (bPoint > aPoint) { // 闲赢
                if (b.getBetPoint().equals(36)) {//买庄
                    return 1;
                }
                if (b.getBetPoint().equals(37)) {//买闲
                    return -1;
                }
            } else if (a.getPoint() == b.getPoint()){ // 同点
                if (game.getDpMax() != null && game.getDpMax() >= b.getPoint()) {//几点一下庄赢，通杀
                    if (b.getBetPoint().equals(36)) {//买庄
                        return -1;
                    }
                    if (b.getBetPoint().equals(37)) {//买闲
                        return 1;
                    }
                }
                double aAmount = a.getAmount().doubleValue();
                double bAmount = b.getAmount().doubleValue();
                switch (game.getSamePoint()) {
                    case "1": // 比金额
                        if (compare(aAmount, bAmount) > 0) {//庄赢
                            if (b.getBetPoint().equals(36)) {//买庄
                                return -1;
                            }
                            if (b.getBetPoint().equals(37)) {//买闲
                                return 1;
                            }
                        } else if (compare(aAmount, bAmount) < 0) {//闲赢
                            if (b.getBetPoint().equals(36)) {//买庄
                                return 1;
                            }
                            if (b.getBetPoint().equals(37)) {//买闲
                                return -1;
                            }
                        } else {
                            return 0;
                        }
                    case "2": // 比角分
                        int aJF = MathUtil.multiply(a.getAmount(), 100).intValue() % 100;
                        int bJF = MathUtil.multiply(b.getAmount(), 100).intValue() % 100;
                        if ("Y".equals(game.getCmpAmount()) && aJF == bJF) { // 比金额
                            if (compare(aAmount, bAmount) > 0) {//庄赢
                                if (b.getBetPoint().equals(36)) {//买庄
                                    return -1;
                                }
                                if (b.getBetPoint().equals(37)) {//买闲
                                    return 1;
                                }
                            } else if (compare(aAmount, bAmount) < 0) {//闲赢
                                if (b.getBetPoint().equals(36)) {//买庄
                                    return 1;
                                }
                                if (b.getBetPoint().equals(37)) {//买闲
                                    return -1;
                                }
                            } else {
                                return 0;
                            }
                        }
                        if (compare(aJF, bJF) > 0) {//庄赢
                            if (b.getBetPoint().equals(36)) {//买庄
                                return -1;
                            }
                            if (b.getBetPoint().equals(37)) {//买闲
                                return 1;
                            }
                        } else if (compare(aJF, bJF) < 0) {//闲赢
                            if (b.getBetPoint().equals(36)) {//买庄
                                return 1;
                            }
                            if (b.getBetPoint().equals(37)) {//买闲
                                return -1;
                            }
                        } else {
                            return 0;
                        }
                    case "3": // 比分
                        int aF = MathUtil.multiply(a.getAmount(), 100).intValue() % 10;
                        int bF = MathUtil.multiply(b.getAmount(), 100).intValue() % 10;
                        if ("Y".equals(game.getCmpFrontOne()) && aF == bF) { // 比角分
                            aJF = MathUtil.multiply(a.getAmount(), 100).intValue() % 100;
                            bJF = MathUtil.multiply(b.getAmount(), 100).intValue() % 100;
                            if (aJF == bJF) { // 比金额
                                if (compare(aAmount, bAmount) > 0) {//庄赢
                                    if (b.getBetPoint().equals(36)) {//买庄
                                        return -1;
                                    }
                                    if (b.getBetPoint().equals(37)) {//买闲
                                        return 1;
                                    }
                                } else if (compare(aAmount, bAmount) < 0) {//闲赢
                                    b.setBetPoint(37);
                                    if (b.getBetPoint().equals(36)) {//买庄
                                        return 1;
                                    }
                                    if (b.getBetPoint().equals(37)) {//买闲
                                        return -1;
                                    }
                                } else {
                                    return 0;
                                }
                            }
                            if (compare(aJF, bJF) > 0) {//庄赢
                                if (b.getBetPoint().equals(36)) {//买庄
                                    return -1;
                                }
                                if (b.getBetPoint().equals(37)) {//买闲
                                    return 1;
                                }
                            } else if (compare(aJF, bJF) < 0) {//闲赢
                                b.setBetPoint(37);
                                if (b.getBetPoint().equals(36)) {//买庄
                                    return 1;
                                }
                                if (b.getBetPoint().equals(37)) {//买闲
                                    return -1;
                                }
                            } else {
                                return 0;
                            }
                        }
                        if (compare(aF, bF) > 0) {//庄赢
                            if (b.getBetPoint().equals(36)) {//买庄
                                return -1;
                            }
                            if (b.getBetPoint().equals(37)) {//买闲
                                return 1;
                            }
                        } else if (compare(aF, bF) < 0) {//闲赢
                            if (b.getBetPoint().equals(36)) {//买庄
                                return 1;
                            }
                            if (b.getBetPoint().equals(37)) {//买闲
                                return -1;
                            }
                        } else {
                            return 0;
                        }
                }
                throw new BusinessException("配置错误");
            }
        } else if (b.getBetPoint().equals(38)) {//和  遇到对子就是和
            if (a.getPoint() == b.getPoint()) {
                return -1;
            } else {
                return 1;
            }
        } else if (b.getBetPoint().equals(39)) {//庄对
            if (a.getPoint().equals(12) || a.getPoint().equals(14) || a.getPoint().equals(15)) {//对子或者满牛或者豹子
                return -1;
            } else {
                return 1;
            }
        } else if (b.getBetPoint().equals(40)) {//闲对
            if (b.getPoint().equals(12) || b.getPoint().equals(14) || b.getPoint().equals(15)) {//对子或者满牛或者豹子
                return -1;
            } else {
                return 1;
            }
        }
        return 0;
    }



    public static String getPoint(Game config, Map<Integer, Power> powers, BigDecimal amount,String type){
        int point = getPoint(config, powers, amount);
        String guessType;
        if (point <= 5 && point > 0) {//小
            guessType= GuessSize.TINYMETHOD;
        } else if (point > 5 && point <= 10) {//大
            guessType= GuessSize.BIGMETHOD;
        } else if (point > 10 && point < 16) {//合
            guessType= GuessSize.HEMETHOD;
        } else if (point == 1 ||point == 3||point == 5||point == 7||point == 9) {//单
            guessType= GuessSize.SINGLEMETHOD;
        } else if (point == 2 ||point == 4||point == 6||point == 8||point == 10) {//双
            guessType=GuessSize.DOUBLEMETHOD;
        } else if ((point == 5||point == 7||point == 9)&&type.equals(GuessSize.BIGSINGLEMETHOD)) {//大单
            guessType= GuessSize.BIGSINGLEMETHOD;
        } else if ((point == 1 ||point == 3)&&type.equals(GuessSize.TINYSINGLEMETHOD)) {//小单
            guessType= GuessSize.TINYSINGLEMETHOD;
        } else if ((point == 6||point == 8||point == 10)&&type.equals(GuessSize.BIGDOUBLEMETHOD)) {//大双
            guessType= GuessSize.BIGDOUBLEMETHOD;
        } else if ((point == 2 ||point == 4)&&type.equals(GuessSize.TINYDOUBLEMETHOD)) {//小双
            guessType= GuessSize.TINYDOUBLEMETHOD;
        } else {
            guessType = "";
        }
        return guessType;
    }
    /**
     * player 中 guessSize 大小单双和，大单，小单，大双，小双比较一样为赢
     * @param game
     * @param player
     * @return 1：庄赢 -1：闲赢 0：玩家抓合
     */
    public static int compare(Game game , Player player){
        Integer point = player.getPoint();//玩家点数
        String guessSize = player.getGuessSize();//玩家猜的 大小单双和
        int result = 0;
        if (point <= 5 && point > 0 && guessSize.equals(GuessSize.TINYMETHOD)) {//小
            result = -1;
        } else if (point > 5 && point <= 10 && guessSize.equals(GuessSize.BIGMETHOD)) {//大
            result = -1;
        } else if (point > 10 && point <= 16 && guessSize.equals(GuessSize.HEMETHOD)) {//合
            result = -1;
        } else if ((point == 1 ||point == 3||point == 5||point == 7||point == 9 )&& guessSize.equals(GuessSize.SINGLEMETHOD)) {//单
            result = -1;
        } else if ((point == 2 ||point == 4||point == 6||point == 8||point == 10) && guessSize.equals(GuessSize.DOUBLEMETHOD)) {//双
            result = -1;
        } else if ((point == 5||point == 7||point == 9)&&guessSize.equals(GuessSize.BIGSINGLEMETHOD)) {//大单
            result = -1;
        } else if ((point == 1 ||point == 3)&&guessSize.equals(GuessSize.TINYSINGLEMETHOD)) {//小单
            result = -1;
        } else if ((point == 6||point == 8||point == 10)&&guessSize.equals(GuessSize.BIGDOUBLEMETHOD)) {//大双
            result = -1;
        } else if ((point == 2 ||point == 4)&&guessSize.equals(GuessSize.TINYDOUBLEMETHOD)) {//小双
            result = -1;
        } else if (point > 10 && point <= 16 && !guessSize.equals(GuessSize.HEMETHOD)) {
            result = 0;
        } else {
            result = 1;
        }
        return  result;
    }

    public static Player compare(Player player){
        Integer point = player.getPoint()+25;//玩家抢包点数
        List<BetPower> betPowerList = player.getBetPowerList();
        for (BetPower power :
                betPowerList) {
            if (point.equals(power.getBetPoint())) {//买中了 至为 胜利
                power.setWin(-1);//猜中
            } else if ( player.getPoint() > 10 && player.getPoint()<=16) {
                power.setWin(0);//合
            } else {
                power.setWin(1);//猜错
            }
        }
        return player;
    }

    /**
     * 比大小
     * point 点数
     * amount 红包金额
     * time 抢红包所用时间
     */
    public static int compare(Player a, Player b, Game game) {
        int aPoint = a.getPoint();
        int bPoint = b.getPoint();
        if (aPoint > bPoint) { // a赢
            return 1;
        } else if (bPoint > aPoint) { // b赢
            return -1;
        } else { // 同点
            double aAmount = a.getAmount().doubleValue();
            double bAmount = b.getAmount().doubleValue();
            if (aAmount != bAmount) {
                switch (game.getSamePoint()) {
                case "1": // 比金额
                    return compare(aAmount, bAmount);
                case "2": // 比时间
                    return compare(b.getTime(), a.getTime());
                }
            } else {
                switch (game.getSamePat()) {
                case "1": // 比时间
                    return compare(b.getTime(), a.getTime());
                case "2": // 下注数
                    return compare(a.getBet(), b.getBet());
                }
            }
            throw new BusinessException("配置错误");
        }
    }

    public static int compare(double a, double b) {
        if (a > b) { // a赢
            return 1;
        } else if (a < b) { // b赢
            return -1;
        } else { // 同点
            return 0;
        }
    }
}
